// pages/pin_J/wxPeopleQuan/wxPeopleQuan.js
let app = getApp();
import util from '../../../utils/util.js';
import WxParse from '../../../libs/wxParse/wxParse.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    painting: {},
    shareImage: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    let id = options.id
    let type = options.type
    // let id = 377
    // let type = 'three'

    let pid = options.pid
    this.setData({
      type,id,pid
    })
    this.getPinInfo()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  //得到产品信息
  getPinInfo() {
    let self = this
    let goodsId = self.data.id 
    let type = self.data.type
    wx.request({
      url: util.apiUrl + 'xcx.pin.product',
      data: {
        product: goodsId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        let error_code = res.data.error_code //错误编码
        let msg = res.data.msg //状态 _ 提示
        //console.log('得到产品信息', res, error_code, msg)//检测数据
        if (error_code == 0) {
          //success
          let product = res.data.product
          // self.setData({
          //   'product': res.data.product
          // })
          self.getPorducts(product)
        } 
      }
    })
  },
  //获取产品分享图皮
  getPorducts(product) {
    let self = this
    wx.request({
      url: util.apiUrl + 'xcx.pin.pintuan',
      data: {
        id: 60
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        let type = self.data.type
        // console.log(type)
        // console.log(product.)
        let Img_product = ''
        if (type == 'three'){
          let tabContentThree = res.data.data['三人拼团']
          console.log(tabContentThree)
          for (var item of tabContentThree){
            if (item.goods_id == product.id){
              // console.log(item.pin_img)
              Img_product = 'https://www.zhenlvw.com/'+item.pin_img
              self.eventDraw(product, Img_product)
            }
          }
        }else{
          let tabContentPeople = res.data.data['人人拼']
          // console.log(tabContentPeople)
          for (var item of tabContentPeople) {
            if (item.goods_id == product.id) {
              Img_product = 'https://www.zhenlvw.com/' + item.pin_img
              self.eventDraw(product, Img_product)
            }
          }
        }
      }
    })
  },
  eventDraw(product, Img_product) {
    wx.showLoading({
      title: '绘制分享图片中',
      mask: true
    })
    let userInfo = wx.getStorageSync('user')
    // console.log(userInfo)
    // console.log('eventDraw',Img_product)
    // console.log('eventDraw', product)
    this.setData({
      painting: {
        width: 375,
        height: 555,
        clear: true,
        views: [
          {
            type: 'image',
            url: '../images/backgroundL.png',
            top: 0,
            left: 0,
            width: 375,
            height: 555
          },
          // {
          //   type: 'image',
          //   url: userInfo.user_img,
          //   top: 27.5,
          //   left: 29,
          //   width: 55,
          //   height: 55
          // },
          // {
          //   type: 'image',
          //   url: '../images/avatar_cover2.png',
          //   top: 27.5,
          //   left: 29,
          //   width: 55,
          //   height: 55
          // },
          {
            type: 'text',
            content: '您的好友【' + userInfo.nickname + '】',
            fontSize: 16,
            color: '#402D16',
            textAlign: 'left',
            top: 33,
            left: 29,
            bolder: true
          },
          {
            type: 'text',
            content: '发现一件好货，邀请您一起享优惠！',
            fontSize: 15,
            color: '#563D20',
            textAlign: 'left',
            top: 59.5,
            left: 29
          },
          // {
          //   type: 'text',
          //   content: '您的好友【' + userInfo.nickname +'】',
          //   fontSize: 16,
          //   color: '#402D16',
          //   textAlign: 'left',
          //   top: 33,
          //   left: 96,
          //   bolder: true
          // },
          // {
          //   type: 'text',
          //   content: '发现一件好货，邀请您一起享优惠！',
          //   fontSize: 15,
          //   color: '#563D20',
          //   textAlign: 'left',
          //   top: 59.5,
          //   left: 96
          // },
          {
            type: 'image',
            url: Img_product,
            top: 136,
            left: 42.5,
            width: 290,
            height: 186
          },
          {
            type: 'image',
            url: '../images/codeShare.jpg',
            top: 443,
            left: 85,
            width: 68,
            height: 68
          },
          {
            type: 'text',
            content: product.name,
            fontSize: 16,
            lineHeight: 21,
            color: '#383549',
            textAlign: 'left',
            top: 336,
            left: 44,
            width: 287,
            MaxLineNumber: 2,
            breakWord: true,
            bolder: true
          },
          {
            type: 'text',
            content: '￥' + product.pin_price,
            fontSize: 19,
            color: '#E62004',
            textAlign: 'left',
            top: 387,
            left: 44.5,
            bolder: true
          },
          {
            type: 'text',
            content: '原价:￥' + product.price,
            fontSize: 13,
            color: '#7E7E8B',
            textAlign: 'left',
            top: 391,
            left: 130,
            textDecoration: 'line-through'
          },
          {
            type: 'text',
            content: '长按识别图中二维码帮我砍个价呗~',
            fontSize: 14,
            color: '#383549',
            textAlign: 'left',
            top: 460,
            left: 165.5,
            lineHeight: 20,
            MaxLineNumber: 2,
            breakWord: true,
            width: 125
          }
        ]
      }
    })
  },
  eventSave() {
    console.log(this.data.shareImage)
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  eventGetImage(event) {
    // console.log('eventGetImage', event)
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  }
})